package flywieight.pattern.demo;

/**
 * @author 逸足天涯
 * @create 2019-10-12 20:15
 */
public class FlyweightPatternEx {
    public static void main(String[] args) throws Exception{
        System.out.println("\n***Flyweight Pattern Example***");

        RobotFactory myFactory = new RobotFactory();
        IRobot shape = myFactory.getRobotFromFactory("small");
        shape.print();

        /* Here we are trying to get the objects additional 2 times,Note that from
        now onward we do not need to create additional small robots as we have
        already created this category
          */

        for (int i = 0; i < 2 ; i++) {
           shape = myFactory.getRobotFromFactory("small") ;
           shape.print();
        }

        int numofDistinctRobots = myFactory.totalObjectCreated();
        System.out.println("\nDistinct Robot objects created till now ="
            + numofDistinctRobots);
         /* Here we are trying to get the objects 5 times,Note that the second time
         onward we do not need to create additional large robots as we have already
         create this category in the first attempt(at i=0)
          */
        for (int i = 0; i < 5 ; i++) {
            shape = myFactory.getRobotFromFactory("large");
            shape.print();
        }
        numofDistinctRobots = myFactory.totalObjectCreated();
        System.out.println("\n Finally no of Distinct Robot objects created :"
                + numofDistinctRobots);
    }
}